package com.example;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

/**
 * Student class - Demonstrates Java object-oriented programming concepts
 * Includes encapsulation, constructors, method overloading, etc.
 */
public class Student {
    // Private attributes - encapsulation
    private String id;
    private String name;
    private int age;
    private String major;
    private List<String> courses;
    private LocalDate enrollmentDate;

    // Constructor - initialize object
    public Student(String id, String name, int age, String major) {
        this.id = id;
        this.name = name;
        this.age = age;
        this.major = major;
        this.courses = new ArrayList<>();
        this.enrollmentDate = LocalDate.now();
    }

    // Overloaded constructor - different initialization options
    public Student(String id, String name) {
        this(id, name, 18, "Undeclared");
    }

    // Getters and Setters - access and modify private attributes
    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        if (age > 0 && age < 120) {
            this.age = age;
        } else {
            System.out.println("Invalid age setting!");
        }
    }

    public String getMajor() {
        return major;
    }

    public void setMajor(String major) {
        this.major = major;
    }

    // Business methods
    public void addCourse(String courseName) {
        if (!courses.contains(courseName)) {
            courses.add(courseName);
            System.out.println("Course added: " + courseName);
        } else {
            System.out.println("Course already exists: " + courseName);
        }
    }

    public void removeCourse(String courseName) {
        if (courses.remove(courseName)) {
            System.out.println("Course removed: " + courseName);
        } else {
            System.out.println("Course not found: " + courseName);
        }
    }

    public void displayCourses() {
        System.out.println(name + "'s Course List:");
        if (courses.isEmpty()) {
            System.out.println("  No courses");
        } else {
            for (int i = 0; i < courses.size(); i++) {
                System.out.println("  " + (i + 1) + ". " + courses.get(i));
            }
        }
    }

    public void displayInfo() {
        System.out.println("=== Student Information ===");
        System.out.println("ID: " + id);
        System.out.println("Name: " + name);
        System.out.println("Age: " + age);
        System.out.println("Major: " + major);
        System.out.println("Enrollment Date: " + enrollmentDate);
        System.out.println("Course Count: " + courses.size());
        System.out.println("========================");
    }

    // Override toString method
    @Override
    public String toString() {
        return "Student{id='" + id + "', name='" + name + "', age=" + age +
               ", major='" + major + "', courses=" + courses.size() + "}";
    }

    // Override equals method
    @Override
    public boolean equals(Object obj) {
        if (this == obj) return true;
        if (obj == null || getClass() != obj.getClass()) return false;

        Student student = (Student) obj;
        return id.equals(student.id);
    }

    // Static method
    public static String generateStudentId(String name, int year) {
        return year + "" + Math.abs(name.hashCode()) % 10000;
    }
}